package org.darcy.service.check.impl;

import java.util.Date;
import java.util.List;

import org.darcy.dao.CheckReasonMapper;
import org.darcy.entity.CheckPlan;
import org.darcy.entity.CheckReason;
import org.darcy.entity.CheckReasonDetail;
import org.darcy.entity.work.WorkInstance;
import org.darcy.framework.constant.SysConstant;
import org.darcy.framework.generic.GenericServiceImpl;
import org.darcy.framework.model.PageData;
import org.darcy.framework.model.PageParam;
import org.darcy.service.check.CheckReasonDetailService;
import org.darcy.service.check.CheckReasonService;
import org.darcy.service.work.WorkService;
import org.darcy.utils.UserTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;

/**
 * CheckReason服务实现
 */
@Service
@Transactional
public class CheckReasonServiceImpl extends GenericServiceImpl<CheckReason> implements CheckReasonService {

	@Autowired
	private CheckReasonMapper mapper;

	@Autowired
	private CheckReasonDetailService CheckReasonDetailService;

	@Autowired
	private WorkService WorkService;

	@Override
	public CheckReason getDetail(Integer id) {
		CheckReason record = mapper.selectById(id);
		record.setDetailList(CheckReasonDetailService.getList(id));
		WorkService.processWorkFlowObject(record, SysConstant.FormType.CheckReason);
		return record;
	}
	
	@Override
	public int delete(Object id) {
		super.delete(id);
		WorkInstance param = WorkInstance.builder().formId((Integer) id).formType(SysConstant.FormType.CheckReason)
				.build();
		WorkInstance work = WorkService.getBusinessWork(param);
		WorkService.delete(work.getId());
		return 1;
	}

	/**
	 * 处理工作任务
	 */
	private void processWork(CheckReason record) {
		String title = String.format("[%s]的%s", UserTool.getLoginUser().getRealName(), "委托依据");
		String summary = String.format("表单编号：%s", record.getFormNo());
		WorkService.addWorkTask(record, title, summary, SysConstant.FormType.CheckReason);
	}

	@Override
	public CheckReason insert(CheckReason record) {
		record.setCreateTime(new Date());
		// 编号处理
		String time = DateUtil.format(record.getCreateTime(), DatePattern.PURE_DATETIME_FORMATTER);
		record.setFormNo("WTYJ" + time);
		super.insert(record);
		List<CheckReasonDetail> detailList = record.getDetailList();
		if (detailList != null) {
			for (CheckReasonDetail detail : detailList) {
				detail.setReasonId(record.getId());
				CheckReasonDetailService.insert(detail);
			}
		}
		processWork(record);
		return record;
	}

	@Override
	public CheckReason update(CheckReason record) {
		super.update(record);
		List<CheckReasonDetail> detailList = record.getDetailList();
		if (detailList != null) {
			for (CheckReasonDetail detail : detailList) {
				if (detail.getId() != null) {
					CheckReasonDetailService.update(detail);
				} else {
					detail.setReasonId(record.getId());
					CheckReasonDetailService.insert(detail);
				}
			}
		}
		processWork(record);
		return record;
	}

	@Override
	public PageData<CheckReason> page(PageParam<CheckReason> param) {
		IPage<CheckReason> queryPage = new Page<>(param.getQuery().getPageNum(), param.getQuery().getPageSize());
		IPage<CheckReason> data = mapper.getPage(queryPage, param.getEntity());
		PageData<CheckReason> pageData = new PageData<CheckReason>(data, param);
		return pageData;
	}

	@Override
	public List<CheckReason> getAll(Integer clientId) {
		QueryWrapper<CheckReason> q = new QueryWrapper<CheckReason>();
		q.eq("client_id", clientId);
		List<CheckReason> list = this.getList(q);
		for (CheckReason a : list) {
			String time = DateUtil.format(a.getCreateTime(), DatePattern.PURE_DATE_FORMATTER);
			a.setDetail("编号：" + a.getFormNo() + ", 委托业务：" + a.getProjectType() + ", 提交时间：" + time);
		}
		return list;
	}

}
